//
//  MapInfoScrollerView.m
//  AppointmentForCar
//
//  Created by TheMacBook on 2016/11/10.
//  Copyright © 2016年 Herron. All rights reserved.
//

#import "MapInfoScrollerView.h"
#import "MapHandleManager.h"
#import "CallCarModel.h"
#import "GeoCodeSearchManager.h"
@interface MapInfoScrollerView () <BMKMapViewDelegate , BMKLocationServiceDelegate , UIGestureRecognizerDelegate>
@end
@implementation MapInfoScrollerView
{
    AdressInfoView * _infoView;
    BMKLocationService * _service; //定位服务
    MapHandleManager * _mapHandle;
    GeoCodeSearchManager * _searchManager;
    
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        self.pagingEnabled = YES;
        self.showsHorizontalScrollIndicator = NO;
        self.bounces = NO;
        self.scrollEnabled = NO;
        _searchManager = [[GeoCodeSearchManager alloc] init];
        _mapHandle = [[MapHandleManager alloc] init];
        BMKMapView * map = [[BMKMapView alloc] init];
        _infoView = [[AdressInfoView alloc] init];
        self.addSubview(map).addSubview(_infoView);
        __weak typeof(self) weakSelf = self;
        [map mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(weakSelf.mas_width);
            make.height.equalTo(weakSelf.mas_height);
            make.right.top.bottom.equalTo(weakSelf);

        }];
        [_infoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(weakSelf.mas_width);
            make.height.equalTo(weakSelf.mas_height);
            make.left.top.bottom.equalTo(weakSelf);
            make.right.equalTo(map.mas_left);

        }];
        _mapHandle.mapView = map;
        _mapView = map;

        [self setup];
        [self addCustomGestures];
    }
    return self;
}
- (void)setup
{
    _service = [[BMKLocationService alloc] init];
    
    //设置代理
    _service.delegate = self;
    
    //开启定位
    [_service startUserLocationService];
    
}

#pragma mark - BMKMapViewDelegateDelegate

/**
 *用户位置更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
    [_service stopUserLocationService];
    [_searchManager geocodeSearchOptionWithAddress:_model.origin results:^(CLLocationCoordinate2D pt) {
            BMKMapPoint point1 = BMKMapPointForCoordinate(userLocation.location.coordinate);
            BMKMapPoint point2 = BMKMapPointForCoordinate(pt);
            CLLocationDistance distance = BMKMetersBetweenMapPoints(point1, point2);
        if([_distanceDelegate respondsToSelector:@selector(getDistance:time:)]) {
//            [_distanceDelegate getDistance:[NSString stringWithFormat:@"距您 %.2f 公里",distance/ 1000.0f] time:[self getNowDate]];
            [_distanceDelegate getDistance:[NSString stringWithFormat:@"距您 %.2f 公里",distance/ 1000.0f] time:[self getNowDate]];

        }
    }];

    
}
- (NSString *)getNowDate {
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm"];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    return dateString;
}

- (void)addCustomGestures {
    /*
     *注意：
     *添加自定义手势时，必须设置UIGestureRecognizer的属性cancelsTouchesInView 和 delaysTouchesEnded 为NO,
     *否则影响地图内部的手势处理
     */
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    doubleTap.delegate = self;
    doubleTap.numberOfTapsRequired = 2;
    doubleTap.cancelsTouchesInView = NO;
    doubleTap.delaysTouchesEnded = NO;
    
    [self addGestureRecognizer:doubleTap];
    /*
     *注意：
     *添加自定义手势时，必须设置UIGestureRecognizer的属性cancelsTouchesInView 和 delaysTouchesEnded 为NO,
     *否则影响地图内部的手势处理
     */
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    singleTap.delegate = self;
    singleTap.cancelsTouchesInView = NO;
    singleTap.delaysTouchesEnded = NO;
    [singleTap requireGestureRecognizerToFail:doubleTap];
    [self addGestureRecognizer:singleTap];
}

- (void)handleSingleTap:(UITapGestureRecognizer *)theSingleTap {
    /*
     *do something
     */
    NSLog(@"my handleSingleTap");
}

- (void)handleDoubleTap:(UITapGestureRecognizer *)theDoubleTap {
    /*
     *do something
     */
    NSLog(@"my handleDoubleTap");
}

#pragma mark - setting and getting 
- (void)setIsMap:(BOOL)isMap
{
//    _isMap = isMap;
    if(_isMap == isMap)
    {
        return;
    }
    _isMap = isMap;
    if (!isMap)
    {
        [self setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    else{
        [self setContentOffset:CGPointMake(self.frame.size.width, 0) animated:YES];
    }
}
- (void)setModel:(CallCarModel *)model
{
    _model = model;
    [_mapHandle planningRouteStartAddr:model.origin endAddr:model.destination];
    _infoView.model = model;

}

- (void)dealloc
{
    if(_mapView)
    {
        _mapView.delegate = nil;
        _mapView = nil;
    }
}

@end


@implementation AdressInfoView
{
    UIImageView * _startImage;
    UIImageView * _endImage;
    UILabel * _startLabel;
    UILabel * _endLabel;
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
//        self.backgroundColor = [UIColor colorWithString:@"#cccccc"];
        self.backgroundColor = kColorBgGray;
        _startImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"locationBegin1"]];
        _endImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"locationEnd1"]];
        _startImage.contentMode = UIViewContentModeCenter;
        _endImage.contentMode = UIViewContentModeCenter;

        _startLabel = [[UILabel alloc] init];
        _endLabel = [[UILabel alloc] init];
        _startLabel.numberOfLines = 2;
        _endLabel.numberOfLines = 2;
        self.addSubview(_startLabel).addSubview(_endLabel).addSubview(_startImage).addSubview(_endImage);
        __weak typeof(self) weakSelf = self;
        [_startImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.mas_top).mas_offset(kScaleHeight(25));
            make.left.equalTo(weakSelf.mas_left).mas_offset(kScaleWidth(30));
            make.size.mas_equalTo((CGSize){kScaleWidth(30),kScaleWidth(50)});
        }];
        [_endImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_startImage.mas_left);
            make.top.equalTo(_startImage.mas_bottom).mas_offset(kScaleHeight(30));
            make.size.mas_equalTo((CGSize){kScaleWidth(30),kScaleWidth(50)});
        }];
        [_startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(_startImage);
            make.left.equalTo(_startImage.mas_right).mas_offset(kScaleWidth(20));
            make.right.equalTo(weakSelf.mas_right).mas_offset(-kScaleWidth(30));
        }];
        [_endLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(_endImage);
            make.left.equalTo(_startImage.mas_right).mas_offset(kScaleWidth(20));
            make.right.equalTo(weakSelf.mas_right).mas_offset(-kScaleWidth(30));
        }];
    }
    return self;
}

- (void)setModel:(CallCarModel *)model {
    _model = model;
    _startLabel.text = model.origin;
    _endLabel.text = model.destination;
}

@end
